/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sbbs.springboot.sample25;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author Administrator
 */
@RestController
@RequestMapping("/spring")
public class ApplicationCtxController implements ApplicationContextAware {

    ApplicationContext ctx = null;

    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        this.ctx = ctx;
    }

    @RequestMapping("/beanNames")
    public String[] getBeanNames() {
        String[] beanNames = ctx.getBeanDefinitionNames();
        System.out.println("所以beanNames个数：" + beanNames.length);
        for (String bn : beanNames) {
            System.out.println(bn);
        }
        return beanNames;
    }

    @RequestMapping("/servs")
    public String[] getServs() {
        String[] beanNames = ctx.getBeanNamesForAnnotation(Service.class);
        System.out.println("所以beanNames个数：" + beanNames.length);
        for (String bn : beanNames) {
            System.out.println(bn);
        }
        return beanNames;
    }

    @RequestMapping("/repos")
    public String[] getRepos() {
        String[] beanNames = ctx.getBeanNamesForAnnotation(Repository.class);
        System.out.println("所以beanNames个数：" + beanNames.length);
        for (String bn : beanNames) {
            System.out.println(bn);
        }
        return beanNames;
    }

    @RequestMapping("/env")
    public Environment getEnv() {
        Environment c = ctx.getEnvironment();

        return c;
    }

}
